﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp_Event
{
    /// <summary>
    /// DrapDropEventWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DrapDropEventWindow : Window
    {
        public DrapDropEventWindow()
        {
            InitializeComponent();
        }

        private void StackPanel_DragEnter(object sender, DragEventArgs e)
        {
            StackPanel? stackPanel = sender as StackPanel;
            if (stackPanel != null)
            {
                stackPanel.Background = Brushes.Gray;
            }
        }

        private void StackPanel_DragOver(object sender, DragEventArgs e)
        {

        }

        private void StackPanel_DragLeave(object sender, DragEventArgs e)
        {
            StackPanel? stackPanel = sender as StackPanel;
            if (stackPanel != null)
            {
                stackPanel.Background = Brushes.Transparent;
            }
        }

        private void StackPanel_Drop(object sender, DragEventArgs e)
        {
            Border moveBorder = (Border)e.Data.GetData(typeof(Border));
            StackPanel? parentStackPanel = moveBorder.Parent as StackPanel;
            StackPanel? stackPanel = sender as StackPanel;
            if (parentStackPanel == null || stackPanel == null)
                return;
            if (stackPanel.Name == "moveStackPanel") 
            {
                parentStackPanel.Children.Remove(moveBorder);
                stackPanel.Children.Add(moveBorder);
                return;
            }
            if (stackPanel.Name == "copyStackPanel") 
            {
                Border border = new Border 
                {
                    Background = moveBorder.Background,
                    Width = moveBorder.Width,
                    Height = moveBorder.Height,
                    CornerRadius = moveBorder.CornerRadius,
                    Margin = moveBorder.Margin
                };
                stackPanel.Children.Add(border);
            }
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Border? border = sender as Border;
            DragDrop.DoDragDrop(border!, border!, DragDropEffects.Move);
        }
    }
}
